{{ define "home/login" }}

{{template "home/header" .}}

<div class="container">
  <div class="columns">
    <div class="column is-4 is-offset-4 login">

      <div class="login-plan">
        <div class="control">
          <h2>{{.title}}</h2>
        </div>
        <div class="control">
          <label for="">邮箱</label>
          <input class="input" type="email" id="email">
        </div>

        <div class="control">
          <label for="">密码</label>
          <input class="input" type="password" id="password">
        </div>

        <div class="control">
          <button class="button is-info is-fullwidth" id="signButton">登录</button>
        </div>
      </div>

      <div class="regist-plan">
        还没有账号， 前去<a href="/oauth/register">注册</a>.
      </div>

    </div>
  </div>
</div>

{{template "home/footer" .}}

<!-- 请求脚本 -->

<script>
  getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  }

  $("#signButton").click(function () {

    var data = {
      email: $("#email").val(),
      password: $("#password").val(),
      client_id: getUrlParam('client_id'),
      return_to: getUrlParam('return_to')
    }

    $.ajax({
      type: 'POST',
      url: "/oauth/authorize",
      data: data,
      success: function (res) {
        if (res.code == 200) {
          window.location.href = res.data.redirect_url;
        } else {
          alert(res.msg);
        }
      },
      dataType: "json"
    })

  })

</script>

{{ end }}
